import subprocess
import tomllib
from pathlib import Path

FILE = Path(__file__).resolve()
ROOT = FILE.parents[2]  # 项目根目录


def build():
    with open(ROOT / "pyproject.toml", "rb") as f:
        config = tomllib.load(f)
    version = config["project"]["version"]

    process = subprocess.Popen(
        [
            "uv",
            "run",
            "nuitka",
            "--onefile",
            "--remove-output",
            "--plugin-enable=pyside6",
            "--windows-console-mode=disable",
            f"--output-filename=DocuKit-windows-64-v{version}",
            "src/docu_kit/main.py",
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,  # 合并错误输出到标准输出
        text=True,  # 返回字符串而非字节
        bufsize=1,  # 行缓冲模式
        shell=True,
    )
    while True:
        line = process.stdout.readline()
        if not line:
            break  # 进程结束
        print(line.strip())  # 实时打印
